package com.kai.steering.common.vo;

import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * @ Author     ：LiuKai.
 * @ Date       ：Created in 10:40 2022-2-22
 * @ Description：信息响应主体
 * @ Modified By：
 * @Version:
 */
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 返回编码：成功标记=0，其他失败
     **/
    @Getter
    @Setter
    private String code;

    /**
     * 返回信息
     **/
    @Getter
    @Setter
    private String message;

    /**
     * 数据
     **/
    @Getter
    @Setter
    private T data;

    public static <T> Result<T> ok() {
        return restResult(null, ReturnCode.SUCCESS.getCode(), null);
    }

    public static <T> Result<T> ok(T data) {
        return restResult(data, ReturnCode.SUCCESS.getCode(), null);
    }

    public static <T> Result<T> ok(T data, String msg) {
        return restResult(data, ReturnCode.SUCCESS.getCode(), msg);
    }
    public static <T> Result<T> judge(Boolean b) {
        if (b==null||!b){
            return failed();
        }else {
            return ok();
        }
    }


    public static <T> Result<T> failed() {
        return restResult(null, ReturnCode.FAIL.getCode(), null);
    }

    public static <T> Result<T> failed(String msg) {
        return restResult(null, ReturnCode.FAIL.getCode(), msg);
    }

    public static <T> Result<T> failed(T data) {
        return restResult(data, ReturnCode.FAIL.getCode(), null);
    }


    private static <T> Result<T> restResult(T data, String code, String msg) {
        Result<T> apiResult = new Result<>();
        apiResult.setCode(code).setData(data).setMessage(msg);
        return apiResult;
    }

    public  Result mergeResult(Result result){

        if (!this.getCode().equals("0")||!result.getCode().equals("0")){
            this.setCode("1");
        }
        this.setMessage(this.getMessage()+result);
        return this;
    }
    public static <T> Result<T> restResult (Result result){
        Result<T> apiResult = new Result<>();
        apiResult.setCode(result.getCode()).setMessage(result.getMessage());
        return apiResult;
    }


}
